package com.tancire.web.controller.market;

import com.tancire.common.core.controller.BaseController;
import com.tancire.common.core.domain.AjaxResult;
import com.tancire.common.core.domain.entity.SysUser;
import com.tancire.common.core.domain.model.LoginUser;
import com.tancire.common.enums.UserSource;
import com.tancire.common.utils.SecurityUtils;
import com.tancire.net.domain.CrmCompany;
import com.tancire.net.domain.UpdatePass;
import com.tancire.net.service.ICrmCompanyService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * 客户管理Controller
 *
 * @author 陈超
 * @date 2023-12-24
 */
@RestController
@RequestMapping("/mkt/crm")
public class CrmCompanyMktController extends BaseController {

    @Resource
    private ICrmCompanyService crmCompanyService;

    /**
     * 获取企业信息
     */
    @PreAuthorize("@ss.hasPermi('company:user:union')")
    @GetMapping(value = "/info")
    public AjaxResult getCompanyInfo() {
        // 获取当前登录账号userId
        Long userId = SecurityUtils.getLoginUser().getUserId();
        CrmCompany crmCompany = crmCompanyService.selectCrmCompanyById(userId);
        SysUser sysUser = new SysUser();
        sysUser.setUserName(crmCompany.getCustomerAccount());
        // sysUser.setPassword(crmCompany.getCustomerPassword());
        sysUser.setNickName(crmCompany.getCustomerName());
        sysUser.setUserId(crmCompany.getId());
        sysUser.setStatus(crmCompany.getCustomerStatus() + "");
        // 标记为企业账号
        sysUser.setUserSource(UserSource.COMPANY.getCode());
        Set<String> prs = new HashSet<>();
        prs.add("company:user:union");
        LoginUser loginUser = new LoginUser(sysUser.getUserId(), 110L, sysUser, prs);
        return AjaxResult.success("查询成功", loginUser);
    }

    /**
     * 获取企业信息
     */
    @PreAuthorize("@ss.hasPermi('company:user:union')")
    @GetMapping(value = "/detail")
    public AjaxResult getCompanyDetail() {
        // 获取当前登录账号userId
        Long userId = SecurityUtils.getLoginUser().getUserId();
        CrmCompany crmCompany = crmCompanyService.selectCrmCompanyById(userId);
        crmCompany.setCustomerPassword(null);
        return AjaxResult.success("查询成功", crmCompany);
    }

    /**
     * 修改客户管理
     */
    @PreAuthorize("@ss.hasPermi('company:user:union')")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody CrmCompany crmCompany) {
        // 当前限制更新部分字段，增加需要在此处添加
        CrmCompany upCompany = new CrmCompany();
        upCompany.setId(SecurityUtils.getUserId());
        upCompany.setCustomerName(crmCompany.getCustomerName());
        upCompany.setLiaison(crmCompany.getLiaison());
        upCompany.setLiaisonPhone(crmCompany.getLiaisonPhone());
        return toAjax(crmCompanyService.updateCrmCompany(upCompany));
    }

    /**
     * 修改客户财务信息
     */
    @PreAuthorize("@ss.hasPermi('company:user:union')")
    @PostMapping("/finance/edit")
    public AjaxResult editFinance(@RequestBody CrmCompany crmCompany) {
        // 当前限制更新部分字段，增加需要在此处添加
        CrmCompany upCompany = new CrmCompany();
        upCompany.setId(SecurityUtils.getUserId());
        upCompany.setBankName(crmCompany.getBankName());
        upCompany.setBankUserName(crmCompany.getBankUserName());
        upCompany.setBankAccount(crmCompany.getBankAccount());
        upCompany.setBankAddress(crmCompany.getBankAddress());
        return toAjax(crmCompanyService.updateCrmCompany(upCompany));
    }

    /**
     * 修改客户财务信息
     */
    @PreAuthorize("@ss.hasPermi('company:user:union')")
    @PostMapping("/bind/app")
    public AjaxResult bindMinApp(@RequestBody CrmCompany crmCompany) {
        crmCompanyService.bindMinApp(crmCompany);
        return AjaxResult.success();
    }

    /**
     * 修改密码
     */
    @PreAuthorize("@ss.hasPermi('company:user:union')")
    @PostMapping("/update/password")
    public AjaxResult updatePassword(@RequestBody UpdatePass updatePass) {
        crmCompanyService.updatePassword(updatePass);
        return AjaxResult.success("修改密码成功");
    }

}
